#include "project.h"
#include "file_loader.h"
#include "structure_amova.h"
#include "structure_gst.h"

Project::Project(QObject *parent ) : QObject(parent) {
}


void Project::setData(DataSet *theData) {
    _theData = theData;
}

ResultsDocument* Project::getResults(int idx) {
    if( idx < _results.count() )
        return _results[idx];
    else
        return new ResultsDocument();
}


void Project::setTreeWidget(QTreeWidget *theTree) {
    _theTree = theTree;


}


/*********************************************************
 *                                                       *
 *                     SLOTS                             *
 *                                                       *
 *********************************************************/

/************************************ FILE STUFF *********/

void Project::slotImportGenotypes() {
    DataImportDialog *dlg = new DataImportDialog(qApp->activeWindow());
    if( dlg->exec() == QDialog::Accepted ){

        if( dlg->count() ) {
            _theData = new DataSet( dlg->getIndividuals() );
            _theData->setPath( dlg->getPath() );
            QFileInfo info( _theData->getPath() );

            _topItem = new QTreeWidgetItem( _theTree );
            _topItem->setIcon(0,QIcon(":/folder_icon"));
            _topItem->setText(0,info.fileName());
            _topItem->setText(0,"New Project");

            _resultsItem = new QTreeWidgetItem();
            _resultsItem->setIcon(0,QIcon(":/folder_icon"));
            _resultsItem->setText(0,tr("Results"));

            _topItem->addChild(_theData->getTreeWidgetItem());
            _topItem->addChild( _resultsItem );

            _theTree->insertTopLevelItem( 0, _topItem );
            _theTree->update();
            qDebug() << "data imported with " << _theData->getLog();
        }
        else
            qDebug() << "dlg->count == 0";
    }
    else
        qDebug() << "dlg exec != accepted";
}


/*********************************** STRUCTURE ANALYSES **/
void Project::slotRunAMOVA() {
    StructureGST *analysis = new StructureGST( _theData );
    if( analysis->selectPartition() ){
        analysis->run();
        _results.append( analysis->getResults() );
        _resultsItem->addChild( analysis->getResults()->getTreeWidgetItem());
        _theTree->update();
    }

}

void Project::slotRunGst() {

}



/************************************ FREQUENCY ANALYSES **/

void Project::slotRunFrequencies() {

}
